const qiniu = require('qiniu');
const fs = require('fs');

var config = new qiniu.conf.Config();
// 空间对应的机房
config.zone = qiniu.zone.Zone_z0;
// 是否使用https域名
config.useHttpsDomain = true;
// 上传是否使用cdn加速
config.useCdnDomain = true;


var accessKey = 'nMfveMwrhhX84JAq7oj7wW3QrXg4-I7InH2vtL3x';
var secretKey = 'Q5bZKI7GYssjWvB9b1fiff6LqinM5D7kvYmutljh';
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

var options = {
    scope: 'findview',
};
var putPolicy = new qiniu.rs.PutPolicy(options);
var uploadToken = putPolicy.uploadToken(mac);


var localFile = "/Users/fjw/IdeaProjects/static/node/demo/tmp/152.png";
var formUploader = new qiniu.form_up.FormUploader(config);
var putExtra = new qiniu.form_up.PutExtra();
var key = '/pinjian/dili_mark/152.png';

function uploadToQiNiu(key, localFile, callback) {

    // 文件上传
    formUploader.putFile(uploadToken, key, localFile, putExtra, function(respErr,
        respBody, respInfo) {
        if (respErr) {
            console.error(localFile);
            throw respErr;
        }

        if (respInfo.statusCode == 200) {
            console.log(respBody);
            callback(respBody.key, localFile);
        } else {
            console.log(respInfo.statusCode);
            console.log(respBody);
        }
    });
}

module.exports = uploadToQiNiu;

// uploadToQiNiu(key, localFile);

//http://img.fundview.cn//pinjian/dili_mark/152.png